import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from '@/store'
import { dlyz } from '@/api/index';
import { Message } from 'element-ui';
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',

    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }, {
    path: '/zhuye',
    name: 'zhuye',
    component: () => import(/* webpackChunkName: "about" */ '../views/zhuye.vue')
  }, {
    path: '/neirong',
    name: 'neirong',
    redirect: '/111',
    component: () => import(/* webpackChunkName: "about" */ '../views/neirong.vue'),
    children: [
      {
        path: '/111',
        name: '111',
        component: () => import(/* webpackChunkName: "about" */ '../views/1.vue'),
        meta: {
          name: '管理员首页',
          icon: 'el-icon-bottom-left'
        }
      }, {
        path: '/222',
        name: '222',
        component: () => import(/* webpackChunkName: "about" */ '../views/2.vue'),
        meta: {
          name: "系管理",
          icon: 'el-icon-bottom-right',
          id: 1

        }
      }, {
        path: '/333',
        name: '333',
        component: () => import(/* webpackChunkName: "about" */ '../views/3.vue'),
        meta: {
          name: "专业管理",
          icon: 'el-icon-back',
          id: 2
        }
      }, {
        path: '/444',
        name: '444',
        component: () => import(/* webpackChunkName: "about" */ '../views/4.vue'),
        meta: {
          name: "班级管理",
          icon: 'el-icon-right',
          id: 4
        }
      }, {
        path: '/555',
        name: '555',
        component: () => import(/* webpackChunkName: "about" */ '../views/5.vue'),
        meta: {
          name: "学生管理",
          icon: 'el-icon-bottom',
          id: 8
        }
      }, {
        path: '/666',
        name: '666',
        component: () => import(/* webpackChunkName: "about" */ '../views/6.vue'),
        meta: {
          name: "教师管理",
          icon: 'el-icon-top',
          id: 16
        }
      }, {
        path: '/777',
        name: '777',
        component: () => import(/* webpackChunkName: "about" */ '../views/7.vue'),
        meta: {
          name: "课程管理",
          icon: 'el-icon-top-left',
          id: 32
        }
      }, {
        path: '/888',
        name: '888',
        component: () => import(/* webpackChunkName: "about" */ '../views/8.vue'),
        meta: {
          name: "选课修改",
          icon: 'el-icon-top-right',
          id: 64
        }
      }, {
        path: '/999',
        name: '999',
        component: () => import(/* webpackChunkName: "about" */ '../views/9.vue'),
        meta: {
          name: "管理员管理",
          icon: 'el-icon-arrow-left',
          id: 128
        }
      }
    ]


  }, {
    path: '/shouye',
    name: 'shouye',
    component: () => import(/* webpackChunkName: "about" */ '../views/shouye.vue'),
    meta: {
      name: "教师",


    },
    children: [
      {
        path: '/shouye12',
        name: 'shouye12',
        component: () => import(/* webpackChunkName: "about" */ '../views/shouye12.vue'),
        meta: {
          name: "首页",

        }
      },
      {
        path: '/shouke',
        name: 'shouke',
        component: () => import(/* webpackChunkName: "about" */ '../views/shouke.vue'),
        meta: {
          name: "授课",

        }
      },
      {
        path: '/kebiao',
        name: 'kebiao',
        component: () => import(/* webpackChunkName: "about" */ '../views/kebiao.vue'),
        meta: {
          name: "课表",

        }
      }
      , {
        path: '/luru',
        name: 'luru',
        component: () => import(/* webpackChunkName: "about" */ '../views/luru.vue'),
        meta: {
          name: "录入",

        }
      }
    ]
  }


]



let originalPush = VueRouter.prototype.push // 先保存一份VueRouter.prototype.push方法
let originalReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function push(location) {
  // call()：篡改上下文    catch()：捕获异常
  return originalPush.call(this, location).catch(e => e)
}

VueRouter.prototype.replace = function replace(location) {
  // call()：篡改上下文    catch()：捕获异常
  return originalReplace.call(this, location).catch(e => e)
}


const router = new VueRouter({

  routes
})

router.beforeEach(async (to, from, next) => {
  console.log(to.meta.id, 321);
  console.log(store.state.wgnd.userInfo, 123);

  if (to.path === '/zhuye') {
    next()
  } else {
    if (store.state.wgnd.userInfo.loggedIn) {


      if (!to.meta.id || store.state.wgnd.userInfo.permissions.indexOf(to.meta.id) != -1) {
        next()
      } else {
        // 没有权限的情况下
        Message({
          message: "该用户没有该权限",
          type: 'error'
        });
        next("/111", () => { })
      }

    } else {
      dlyz().then(res => {
        console.log(res, '验证');

        if (res.data.loggedIn) {

          store.commit('wgnd/gabianuserInfo', res.data)
          let privileges = [];
          for (let bit = 0; bit < 8; bit++) {
            console.log(res, 98);
            if (((1 << bit) & res.data.permission) === 1 << bit) {
              privileges.push((1 << bit));
            }
          }
          console.log(privileges);
          res.data["permissions"] = privileges

          next(to.path, () => { })
        } else {
          next('/zhuye')
        }

      })
    }
  }

})



export default router
